package ${packageName}.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import ${packageName}.model.dto.${name}DTO;
import ${packageName}.model.vo.${name}VO;
import ${packageName}.service.wrapper.${name}ServiceWrapper;
import com.chris.cc.common.web.R;
import java.util.Map;
import org.springframework.data.domain.PageRequest;

/**
* Copyright (c) 2018-2025, chris All rights reserved.
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* <p>
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* <p>
* Author: ${author}
* <p>
* Date: ${time}
* ${description}
**/
@RestController
@RequestMapping("/${lowerName}")
@Slf4j
public class ${name}Controller {
    @Autowired
    private ${name}ServiceWrapper ${lowerName}ServiceWrapper;

    /**
    *  查询
    * @param page
    * @param limit
    * @return Page
    */
    @GetMapping
    public R query(int page, int limit) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        return R.builder().data(${lowerName}ServiceWrapper.query(pageRequest)).build();
    }


    /**
     * 根据id获取
     * @param id
     * @return R
     */
    @GetMapping("/{id}")
    public R get(@PathVariable("id") ${primaryFieldType} id){
        return R.builder().data(${lowerName}ServiceWrapper.get(id)).build();
    }

    /**
     * 添加
     * @param ${lowerName}
     * @return R
     */
    @PostMapping
    public R add(@RequestBody ${name}DTO ${lowerName}){
		try {
            ${lowerName}ServiceWrapper.add(${lowerName});
            return R.builder().build();
        } catch (Exception e) {
            log.error("add ${lowerName} failed.", e);
            return R.builder().ret(-1).build();
        }
    }

    /**
     * 更新
     * @param ${lowerName}
     * @return R
     */
    @PutMapping
    public R update(@RequestBody ${name}DTO ${lowerName}){
        try {
            ${lowerName}ServiceWrapper.update(${lowerName});
            return R.builder().build();
        } catch (Exception e) {
            log.error("update ${lowerName} failed.", e);
            return R.builder().ret(-1).build();
        }
    }

    /**
     * 删除
     * @param id
     * @return R
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable ${primaryFieldType} id){
      try {
            ${lowerName}ServiceWrapper.delete(id);
            return R.builder().build();
        } catch (Exception e) {
            log.error("delete ${lowerName} failed", e);
            return R.builder().ret(-1).msg(e.getMessage()).build();
        }
    }
}
